package com.wyr.admin.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.wyr.admin.service.UserInfoService;
import com.wyr.common.annotation.Log;
import com.wyr.common.annotation.RateLimit;
import com.wyr.common.vo.Result;
import com.wyr.dao.entity.SysUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理
 *
 * @author wangyiren
 * @version 1.0
 * &#064;title UserController
 * &#064;description
 * &#064;date 2025/03/06 14:37
 */
@Slf4j
@Tag(name = "用户管理")
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 用户登录
     *
     * @param username
     * @param password
     * &#064;description
     * @return: com.wyr.common.vo.Result<?>
     * @author wangyiren
     * &#064;date 2025/3/21 09:55
     */
    @RateLimit(qps = 2, timeout = 1)
    @Operation(summary = "用户登录")
    @Parameters({
            @Parameter(name = "username", description = "账号", required = true, in = ParameterIn.QUERY),
            @Parameter(name = "password", description = "密码", required = true, in = ParameterIn.QUERY),
    })
    @GetMapping("doLogin")
    public Result<?> doLogin(String username, String password) {
        return userInfoService.userLogin(username, password);
    }

    /**
     * 判断用户是否登录
     *
     * &#064;description
     * @return: com.wyr.common.vo.Result<?>
     * @author wangyiren
     * &#064;date 2025/3/10 13:06
     */
    @Log
    @Operation(summary = "是否登录")
    @GetMapping("isLogin")
    public Result<?> isLogin() {
        return Result.ok(StpUtil.isLogin());
    }

    /**
     * 分页查询用户
     *
     * @param pageNum
     * @param pageSize
     * &#064;description
     * @return: com.wyr.common.vo.Result<?>
     * @author wangyiren
     * &#064;date 2025/3/21 09:56
     */
    @RateLimit(qps = 2, timeout = 1)
    @Operation(summary = "分页查询用户")
    @Parameters({
            @Parameter(name = "pageNum", description = "当前页", required = true, in = ParameterIn.QUERY),
            @Parameter(name = "pageSize", description = "页数量", required = true, in = ParameterIn.QUERY),
            @Parameter(name = "name", description = "用户名称", required = false, in = ParameterIn.QUERY),
    })
    @GetMapping("listUserByPage")
    public Result<?> listUserByPage(int pageNum, int pageSize, String name) {
        return userInfoService.listUserByPage(pageNum, pageSize, name);
    }

    /**
     * 用户新增
     *
     * @param user
     * &#064;description
     * @return: com.wyr.common.vo.Result<?>
     * @author wangyiren
     * &#064;date 2025/3/21 10:02
     */
    @Operation(summary = "用户新增")
    @PostMapping("saveUser")
    public Result<?> saveUser(@RequestBody SysUser user) {
        return userInfoService.saveUser(user);
    }


}
